import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '../../../../../node_modules/@angular/router';
import { HttpService, PagedResultDto, ExpressionPair, Compare, AndOr, PagerSearchInput } from '../../..';
import { switchMap } from '../../../../../node_modules/rxjs/operators';
import { NzMessageService } from '../../../../../node_modules/ng-zorro-antd';

@Component({
  selector: 'app-student-score-set',
  templateUrl: './student-score-set.component.html',
  styleUrls: ['./student-score-set.component.scss']
})
export class StudentScoreSetComponent implements OnInit {
  // 项目ID
  selectProjectId;
  // 班级ID
  selectClassId;

  listSearchValue; // 过滤条件
  selectId; // 要修改成绩的条目ID

  score; // 设置报告的成绩。用于弹出窗。
  teacherComment = ''; // 教师评语

  showScoreModule = false;
  loadingData = false;
  /**
 * 分页数据属性
 */
  listEntities = new PagedResultDto<any>(0, 0, 1, 10, new Array<any>());


  constructor(
    private route: ActivatedRoute,
    private http: HttpService,
    private _message: NzMessageService,
  ) {

  }

  ngOnInit() {
    this.listEntities.pageSize = 12;
    // this.route.paramMap.pipe(
    //   switchMap((params: ParamMap) => {
    //     this.selectProjectId = +params.get('id');
    //     this.selectClassId = +params.get('classid');
    //     return this.getList();
    //   })
    // ).subscribe();
    this.route.queryParams.subscribe(e => {
      this.selectProjectId = +e.id;
      this.selectClassId = +e.classid;
      return this.getList();
    });
  }

  getIndex(index: number) {
    index += 1;
    let reslut = index > 4 ? index % 4 : index;
    reslut = reslut === 0 ? reslut + 1 : reslut;
    return reslut;
  }

  async getList() {
    this.loadingData = true;
    this.listSearchValue = new Array<ExpressionPair>(
      new ExpressionPair('projectid', this.selectProjectId, Compare.Equal, AndOr.And),
      new ExpressionPair('classid', this.selectClassId, Compare.Equal, AndOr.And),
    );
    const searchBody = new PagerSearchInput(
      this.listEntities.currentPageIndex,
      this.listEntities.pageSize,
      this.listSearchValue,
      []);
    this.listEntities = await this.http.post<any>(`teacherinfo/TeacherClassStudentDrillSubjects`, searchBody);
    this.loadingData = false;
  }

  async setReportScore() {
    if (this.selectId) {
     if (!this.teacherComment) {
        this._message.error('请填写评语');
      } else {
        await this.http.get<any>(`teacherinfo/TeacherSetStuedentSubjectReportScore?id=${this.selectId}&score=${this.score}&reportComment=${this.teacherComment}`);
        this._message.success('设置成功');
        this.getList();
        this.showScoreModule = false;
      }
    }
  }

  async show(id: any, score: number, py: string) {
    this.selectId = id;
    this.score = score;
    this.teacherComment = py;
    this.showScoreModule = true;
  }
}
